import pygame, math

class Paddle():
    def __init__(self, image, speed, screenSize):
        self.surface = pygame.image.load(image)
        self.rect = self.surface.get_rect()
        self.speed = [0,0]
        self.radius = self.rect.width/2.25
        self.maxSpeed = speed
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.didBounce = False
    
    def move(self):
        self.didBounce = False
        self.rect = self.rect.move(self.speed)
    
    def direction(self, dir):
        print dir
        if dir == "up":
            self.speed[1] = -self.maxSpeed[1]
        elif dir == "down":
            self.speed[1] = self.maxSpeed[1]
        elif dir == "stop up":
            self.speed[1] = 0
        elif dir == "stop down":
            self.speed[1] = 0
        
        if dir == "right":
            self.speed[0] = self.maxSpeed[0]
        elif dir == "left":
            self.speed[0] = -self.maxSpeed[0]
        elif dir == "stop right":
            self.speed[0] = 0
        elif dir == "stop left":
            self.speed[0] = 0
        print self.speed
       
        
    def place(self, pt):
        self.rect = self.rect.move(pt)
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

    
    def wallCollide(self):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-2
            self.move()
            self.speed = [0,0]
            self.didBounce = True
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = self.speed[1]*-1
            self.move()
            self.speed = [0,0]
            self.didBounce = True
            
    def ballCollide(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):
                    if not self.didBounce:
                        self.speed[0] = self.speed[0] * -2                        
                        self.speed[1] = self.speed[1] * -2
                        self.move()
                        self.speed = [0,0]
                        self.didBounce = True
                    if not other.didBounce:
                        other.speed[0] = other.speed[0] * -1
                        other.speed[1] = other.speed[1] * -1
                        other.didBounce = True
                    return True
        return False
    